package com.yijiu.trb;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.text.style.URLSpan;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;

import com.yijiu.action.BaseAction;
import com.yijiu.network.Request;
import com.yijiu.network.StringUtils;
import com.yijiu.resource.ActionType;
import com.yijiu.resource.C;
import com.yijiu.resource.CacheData;
import com.yijiu.response.BaseResponse;
import com.yijiu.response.RequestAccountInfoResponse;
import com.yijiu.utils.DialogUtils;

/**
 * 确认投资
 * 
 * @author cd_hxj
 */
public class DoInvestActivity extends BTitleActivity
{
	private EditText moneyEditText, moneyAgainEditText, payPwdEditText;

	// private DataSP base = new DataSP(this, C.BASE_XML_NAME);
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_do_invest);
		findViewsInit();
	}

	private void findViewsInit()
	{
		titleView.setText("确认投资");
		moneyEditText = (EditText) findViewById(R.id.et_do_invest_set_money);
		moneyAgainEditText = (EditText) findViewById(R.id.et_do_invest_set_money_again);
		payPwdEditText = (EditText) findViewById(R.id.et_do_invest_pay_pwd);
		SpannableString sp = new SpannableString("我已阅读并同意《投资权益回购覆约担保合同》");
		TextView agreeTextView = (TextView) findViewById(R.id.tv_do_invest_info);
		sp.setSpan(new CheckAgreeText(""), 8, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		sp.setSpan(new ForegroundColorSpan(Color.BLACK), 8, 20, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		agreeTextView.setText(sp);
		agreeTextView.setMovementMethod(LinkMovementMethod.getInstance());
		CheckBox isAgreeCheckBox = (CheckBox) findViewById(R.id.cb_do_invest_isagree);
		final Button btnDoInvest = (Button) findViewById(R.id.btn_do_invest_submit);
		isAgreeCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
			{
				if (isChecked)
				{
					btnDoInvest.setEnabled(true);
					btnDoInvest.setBackgroundResource(R.drawable.login_background);
				}
				else
				{
					btnDoInvest.setEnabled(false);
					btnDoInvest.setBackgroundResource(R.drawable.btn_login_bg_false);
				}
			}
		});
	}

	/**
	 * 请求项目列表
	 */
	private void requestProjectList()
	{
		String moneyStr = moneyEditText.getText().toString();
		String moneyAgainStr = moneyAgainEditText.getText().toString();
		String payPwdStr = payPwdEditText.getText().toString();
		if (StringUtils.isNull(moneyStr))
		{
			app.showMsg("请输入投资金额");
			return;
		}
		if (StringUtils.isNull(moneyAgainStr))
		{
			app.showMsg("请再次输入投资金额");
			return;
		}
		if (!moneyStr.equals(moneyAgainStr))
		{
			app.showMsg("两次输入的投资金额不一样");
			return;
		}
		if (StringUtils.isNull(payPwdStr))
		{
			app.showMsg("请输入支付密码");
			return;
		}
		String tradeIdStr = getIntent().getStringExtra("tradeId");
		String tokenStr = getIntent().getStringExtra("token");
		// 将用户输入的信息拼接成字符串发送到服务器
		String inputStr = "service=appInvest&amount=" + moneyAgainStr + "&password=" + payPwdStr + "&tradeId=" + tradeIdStr
				+ "&token=" + tokenStr;
		Log.e("result", "---" + inputStr);
		Log.e("result", "@@@" + tokenStr);
		// 私钥
		// 用私钥进行AES加密
		encryptBase(inputStr, C.PRIVATE_KEY);
		// 将用公钥加密后的私钥、用私钥加密后的数据进行封装
		BaseAction requestData = new BaseAction(encodedData, encriptData, CacheData.APP_KEY);
		// 登录响应
		BaseResponse baseResponse = new BaseResponse();
		// 请求服务器
		Request request = new Request(requestData, baseResponse, ActionType.DO_INVEST);
		netManager.excute(request, this, this);
		showProgress("正在提交投资数据，请稍候");
	}

	/**
	 * 账户信息请求
	 * 
	 * @param appKey
	 */
	private void requestAccountInfo(String appKey)
	{
		String inputStr = "service=queryAcountInfo";
		encryptBase(inputStr, C.PRIVATE_KEY);
		BaseAction action = new BaseAction(encodedData, encriptData, appKey);
		RequestAccountInfoResponse requestAccountInfoResponse = new RequestAccountInfoResponse();
		Request request = new Request(action, requestAccountInfoResponse, ActionType.REQUEST_ACCOUNT_INFO);
		netManager.excute(request, this, this);
		showProgress("正在获取账户信息");
	}

	/* 操作成功返回的函数 */
	@Override
	public void getResult(Request r)
	{
		super.getResult(r);
		int type = r.getType();
		switch (type)
		{
			case ActionType.DO_INVEST:
				app.showMsg("投资成功！");
				requestAccountInfo(CacheData.APP_KEY);
				setResult(RESULT_OK);
				finish();
				break;
			case ActionType.REQUEST_ACCOUNT_INFO:
				RequestAccountInfoResponse requestAccountInfoResponse = (RequestAccountInfoResponse) r.getResponse();
				CacheData.AVAILABLE_BALANCE = requestAccountInfoResponse.field.availableBalance.toString();
				CacheData.CERTIFY_STATUS = requestAccountInfoResponse.field.certifyStatus;
				break;
		}
	}

	/* 操作失败返回的函数 */
	@Override
	public void notifyError(int type, int errorCode, String errorMsg)
	{
		super.notifyError(type, errorCode, errorMsg);
		switch (type)
		{
			case ActionType.DO_INVEST:
			case ActionType.REQUEST_ACCOUNT_INFO:
				app.showMsg(errorMsg);
				break;
		}
	}

	/* 重复提交返回的函数 */
	@Override
	public void rePost(Request r)
	{
		super.rePost(r);
	}

	class CheckAgreeText extends URLSpan
	{
		public CheckAgreeText(String src)
		{
			super(src);
		}

		@Override
		public void onClick(View widget)
		{
			DialogUtils.showAgreeTxtDialog(DoInvestActivity.this, R.string.txt3);
		}
	}

	public void doInvestClick(View v)
	{
		int id = v.getId();
		switch (id)
		{
			case R.id.btn_do_invest_submit:
				requestProjectList();
				break;
			default:
				break;
		}
	}
}
